package com.googlecode.jascii.printing;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.io.File;
import java.io.IOException;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;
import javax.swing.RepaintManager;

import org.apache.commons.io.FileUtils;

import com.googlecode.jascii.TextReportReader;

public class TextReportPrinter implements Printable {
	private TextReportReader textReader;
	
	private int ipageStart = 0;
	private int ipageEnd = 1;
	private float fontSize;
	
	public TextReportPrinter(File txtFile  , int fontSize , String encode) throws IOException {
		super();
		textReader = new TextReportReader(txtFile,encode);
		this.fontSize = fontSize;
	}
	public TextReportPrinter(File txtFile  , int fontSize ) throws IOException {
		super();
		textReader = new TextReportReader(txtFile,null);
		this.fontSize = fontSize;
	}
	public TextReportPrinter(String txtContent  , int fontSize ) {
		super();
		textReader = new TextReportReader(txtContent);
		this.fontSize = fontSize;
		
	}
//	public TextReportPrinter(String txtContent  , int fontSize , String encode) {
//		super();
//		textReader = new TextReportReader(txtContent);
//		this.fontSize = fontSize;
//	}
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
			throws PrinterException {

		Graphics2D g2 = (Graphics2D) graphics; 
		g2.setPaint(Color.black); 
		System.out.println("1 pageIndex="+pageIndex);
		System.out.println("1 ipageEnd="+ipageEnd);
		if(pageIndex >= ipageEnd || pageIndex >= textReader.getPages())
			return Printable.NO_SUCH_PAGE;
		
		System.out.println("ipageStart="+ipageStart);
		g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
		Font fireflyFontDynamic = FontUtils.getFont("fireflysung.ttf");
		Font fireflyFont = fireflyFontDynamic.deriveFont(fontSize  );

		g2.setFont(fireflyFont); 
		
		String page = textReader.getPage(ipageStart + pageIndex);
		//System.out.println(page);
		String[] lineTxt = page.split("\n");
		
		System.out.println(lineTxt.length);
		int ix=0;
		float y = fontSize;
		for(int iy = 0;iy< lineTxt.length;iy++)
		{
			String line = lineTxt[iy];
			
			for(int itemp=0;itemp<line.length();itemp++)
		      {
		    	  char ich = line.charAt(itemp);
		    	  String istr = String.valueOf(ich);
		    	  
		    	  if(istr.getBytes().length==1)
		    	  {
		    		  g2.drawString(istr, (int)ix, (int)y);
		    	      ix+=fontSize/2;
		    	  }
		    	  else
		    	  {
		    		  g2.drawString(istr, (int)ix, (int)y);
		    		  ix+=fontSize;
		    	  }
		    		  
		      }
				
			  y += fontSize;
		      ix=0;
			
		}
		
		return Printable.PAGE_EXISTS; 
	}
	public void printPage() throws PrinterNotFoundException, IOException
	{
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
		pras.add(MediaSizeName.ISO_A4); 
		pras.add(new Copies(1));
		printPage(null,pras);
	}
	public void printPage(String prnName , int pageStart,int pageEnd) throws PrinterNotFoundException, IOException
	{
		this.ipageStart = pageStart;
		this.ipageEnd = pageEnd;
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
		pras.add(MediaSizeName.ISO_A4); 
		pras.add(new Copies(1));
		printPage(prnName,pras);
	}
	public void printPage( String prnName , int pageStart,int pageEnd,PrintRequestAttributeSet pras) throws PrinterNotFoundException, IOException
	{   
		this.ipageStart = pageStart;
		this.ipageEnd = pageEnd;
		printPage(prnName,pras);
	}
	public void printPage( int pageStart,int pageEnd) throws PrinterNotFoundException, IOException
	{
		this.ipageStart = pageStart;
		this.ipageEnd = pageEnd;
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
		pras.add(MediaSizeName.ISO_A4); 
		pras.add(new Copies(1));
		printPage(null,pras);
	}
	public void printPage( int pageStart,int pageEnd,PrintRequestAttributeSet pras) throws PrinterNotFoundException, IOException
	{
		this.ipageStart = pageStart;
		this.ipageEnd = pageEnd;
		printPage(null,pras);
	}
	public void printPage(String prnName ) throws PrinterNotFoundException, IOException
	{
		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
		pras.add(MediaSizeName.ISO_A4); 
		pras.add(new Copies(1));
		printPage(prnName,pras);
	}
	public void printPage(String prnName ,PrintRequestAttributeSet pras) throws PrinterNotFoundException, IOException
	{
		if(ipageStart==1)
		{
			ipageStart--;
			ipageEnd--;
		}
		ipageEnd ++;
		DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE; 
		PrintService printService  =  FoundPrinter.getNamedPrintService(prnName) ; 
		DocPrintJob job = printService.createPrintJob(); 
		job.addPrintJobListener(new PrintJobAdapter() {
            public void printDataTransferCompleted(PrintJobEvent pje) {
               System.out.println("Print Job Completed");
            }
        });
		DocAttributeSet das = new HashDocAttributeSet(); 
		SimpleDoc doc = new SimpleDoc(this, flavor, das); 
	
		try { 
				job.print(doc, pras);
		} catch (PrintException pe) { 
		pe.printStackTrace(); 
		} 
		
	}
	
	

}
